﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using MindBreaker.LevelManagement;

namespace MindBreaker.GameObjects
{
    public class Ball : PhysicObject
    {
        private const String BALL_TEXTURE_KEY = "DefaultBall";
        private const float BALL_SPEED_POWER = 2.5f;
        private bool initialized;
        private SoundEffect ballBounceCase;
        private SoundEffect ballBounceBar;
        private SoundEffect ballDestruction;

        // Constructor
        public Ball(Game game, int posX, int posY)
            : base(game, BALL_TEXTURE_KEY, posX, posY, 0, 0)
        {
            this.initialized = false;
        }


        public override void Initialize()
        {
            base.Initialize();
        }

        //Surcharge de la méthode loadcontent
        protected override void LoadContent()
        {
            this.BallBounceCase = this.Game.Content.Load<SoundEffect>(@"sounds\soundPoterieBreak");
            this.BallBounceBar = this.Game.Content.Load<SoundEffect>(@"sounds\soundBarHit");
            this.ballDestruction = this.Game.Content.Load<SoundEffect>(@"sounds\soundBallDestruction");
            base.LoadContent();
        }

        //Mouvement de la balle
        public void checkOutOfArea()
        {
            int isOutArea = this.isOutOfArea();
            if (isOutArea != 0) {
                if (isOutArea == -2) {
                    this.DestroyGameObject();
                }  else if (isOutArea > 0) {
                    this.reverseSpeedX();
                } else {
                    this.reverseSpeedY();
                }
            }
        }

        //Surcharge des collisions pour jouer le son de la balle
        public override int collisionWith(GameObject obj) {

            //Instance de son
            SoundEffectInstance soundInstance;

            int resultatCollision = base.collisionWith(obj);
            if (resultatCollision != 0)
            {
                if (obj is BottomBar)
                {
                    soundInstance = this.BallBounceBar.CreateInstance();
                    soundInstance.Volume = TextureManager.SOUND_VOLUME_EFFECT;
                    soundInstance.Play();
                }
                else if (obj is LevelCase)
                {
                    soundInstance = this.BallBounceCase.CreateInstance();
                    soundInstance.Volume = TextureManager.SOUND_VOLUME_EFFECT;
                    soundInstance.Play();
                }

            }
            return resultatCollision;
        }

        public override void Update(GameTime gameTime)
        {
            if (!this.initialized) {
                KeyboardState keyboard = Keyboard.GetState();
                if (keyboard.IsKeyDown(Keys.Space)) {
                    // Envoi la balle dans une diagonale aléatoire
                    Random randomGenerator = new Random();
                    int rdmX = (randomGenerator.Next(2) == 0) ? -1 : 1;
                    //int rdmY = (randomGenerator.Next(2) == 0) ? -1 : 1;

                    this.SpeedX = (float)(BALL_SPEED_POWER / Math.Sqrt(2.0d)) * rdmX;
                    this.SpeedY = (float)(BALL_SPEED_POWER * -1.0f / Math.Sqrt(2.0d));
                    
                    this.initialized = true;
                }
            }

            this.checkOutOfArea();
            base.Update(gameTime);
        }

        public override void DestroyGameObject()
        {
            SoundEffectInstance soundInstance = this.ballDestruction.CreateInstance();
            soundInstance.Volume = TextureManager.SOUND_VOLUME_EFFECT;
            soundInstance.Play();
            base.DestroyGameObject();
        }

        public SoundEffect BallBounceCase {
            get { return ballBounceCase; }
            set { ballBounceCase = value; }
        }

        public SoundEffect BallBounceBar {
            get { return ballBounceBar; }
            set { ballBounceBar = value; }
        }
    }
}
